require(['grid', 'topBar', 'layout'], function () {
    var editDialog,// 黑点地图区域设置-对话框
        funName,
        mapDistriDialog,// 黑点分布地图-对话框
        $top = $('#top'),
        $grid = $('#grid');

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '黑点名称',
                    name: 'blackName',
                    width: 165,
                    type: 'text'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                        reloadGridData();
                    }
                },
                {
                    text: '黑点分布地图',
                    type: 'btn',
                    css: 'map-btn',
                    click: function () {
                        showBlcSpot2Map();
                    }
                }

            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                                exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                                exportFile('Pdf');
                            }
                        }
                    ]
                },
                {
                    text: '+添加黑点',
                    type: 'btn',
                    authority: '',
                    click: function () {
                        var option = {};
                        option.title = '添加黑点';

                        getEditDialog('add', option).dialog('exec', 'clean');
                    }
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/baseBlackArchivesInfo/searchBaseBlackArchivesInfoPage.htm",
            colModel: [
                {name: "rcrdId", label: 'rcrdId', key: true, hidden: true},
                {name: "blackName", label: '黑点名称'},
                {name: "blackNature", width: 80, label: '黑点性质', dict: 'BLAK0000'},
                {
                    name: "dangerDegree", label: '危险度', width: 80, align: 'center',
                    formatter: function (value) {
                        var html = '<span class="dangerous-setting">';
                        switch (value) {
                            case 1:
                                html += '<span class="active"></span><span></span><span></span><span></span><span></span>';
                                break;
                            case 2:
                                html += '<span class="active"></span><span class="active"></span><span></span><span></span><span></span>';
                                break;
                            case 3:
                                html += '<span class="active"></span><span class="active"></span><span class="active"></span><span></span><span></span>';
                                break;
                            case 4:
                                html += '<span class="active"></span><span class="active"></span><span class="active"></span><span class="active"></span><span></span>';
                                break;
                            case 5:
                                html += '<span class="active"></span><span class="active"></span><span class="active"></span><span class="active"></span><span class="active"></span>';
                                break;
                            default :
                                html += '<span></span><span></span><span></span><span></span><span></span>';
                                break;
                        }
                        html += '</span>';
                        return html;
                    }
                },
                {name: "highestSpdOfMotVhi", width: 80, label: '最高限速', align: 'right'},
                {name: "areaRoad", label: '区域路段'},
                {
                    name: "operate", width: 80, label: '操作', title: false, align: 'center',
                    formatter: 'operArea',
                    formatoptions: [/*{
                        text: '明细',
                        css: 'operate-detail',
                        click: function (data) {
                            var option = {};
                            option.title = '黑点信息';
                            getEditDialog('edit', option).dialog('exec', 'setFormValues', data.rcrdId);
                        }
                    }, */{
                        text: '编辑',
                        css: 'operate-edit',
                        click: function (data) {
                            var option = {};
                            option.title = '编辑黑点';
                            getEditDialog('edit', option).dialog('exec', 'setFormValues', data.rcrdId);
                        }
                    }, {
                        text: '删除',
                        css: 'operate-delete',
                        click: function (data) {
                            $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                                if (r) {
                                    $.ajaxExt({
                                        url: "/baseBlackArchivesInfo/delBaseBlackArchivesInfoById.htm",
                                        data: {'rcrdId': data.rcrdId}
                                    }).done(function () {
                                        $grid.trigger("reloadGrid");
                                    });
                                }
                            });
                        }
                    }]

                }
            ],
            multiselect: true
        });
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog(flag, option) {
        funName = flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '黑点信息',
                url: './safety/setting/funBlackspotSetEdit.htm',
                height: $(window.parent).height() * 0.9,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                            var _t = this;
                            $(_t).dialog('exec', funName, function () {
                                reloadGridData();
                                $(_t).dialog('close');
                            });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }

    /**
     * 获取地图分布对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getMapDistriDialog(option) {
        option = option || {};
        if (mapDistriDialog) {
            mapDistriDialog.dialog('open');
            if (option.title) {
                mapDistriDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            mapDistriDialog = $('<div>').dialog({
                title: option.title || '地图分布',
                url: './safety/setting/funBlackspotMap1.htm',
                height: $(window.parent).height() * 0.9,
                width: $(window.parent).width() * 0.9,
                autoOpen: true,
                buttons: []
            });
        }
        return mapDistriDialog;
    }

    //查询grid
    function reloadGridData() {
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: $top.getValues()
        }).trigger("reloadGrid");
    }

    //地图上显示勾选的黑点
    function showBlcSpot2Map() {
        var list = [];
        
        var rowIds = $grid.grid('getGridParam', 'selarrrow');
        
        console.log(rowIds);
        
        if (rowIds && rowIds.length > 0) {
            //弹框显示
            var option = {};
            option.title = '黑点分布地图';
            getMapDistriDialog(option).dialog('exec', 'showData', rowIds);
        } else {
            $.messager.alert("警告", "请先勾选黑点，再进行操作！", "info");
        }
    }

    //导出
    function exportFile(suffix) {
        if ($grid.getGridParam("reccount") > 0) {
            var postData = $top.parent().getValues();
            $.exportFile("/baseBlackArchivesInfo/exportBaseBlackArchivesInfo" + suffix + ".htm", postData);
        }
        else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }

    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();

    //进页面后查询
    reloadGridData();
});